package com.magic.smartcity.view.more.adapters

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.magic.smartcity.R
import com.magic.smartcity.beans.ActivityDetail
import com.magic.smartcity.databinding.ItemActivityHomeListBinding
import com.magic.smartcity.utils.Constant

/**
 * @description:
 * @create: Create by Magic on 2022/2/16.
 */
class ActivityHomeListAdapter : RecyclerView.Adapter<ActivityHomeListAdapter.ItemHolder>() {
    private val TAG: String = "ActivityHomeListAdapter"
    private val list: MutableList<ActivityDetail> = mutableListOf()

    class ItemHolder(binding: ItemActivityHomeListBinding) : RecyclerView.ViewHolder(binding.root) {
        val binding = binding
    }

    fun setData(listData:List<ActivityDetail>) {
        list.clear()
        list.addAll(listData)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val binding = DataBindingUtil.inflate<ItemActivityHomeListBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_activity_home_list,
            parent,
            false
        )
        return ItemHolder(binding)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.binding.activityDetail = list[position]
        holder.binding.root.setOnClickListener{
            var bundle=Bundle()
            bundle.putInt("id",list[position].id)
            Navigation.findNavController(it).navigate(R.id.activityDetailFragment,bundle)
        }
        Glide.with(holder.binding.imageView).load(Constant.Service_IP+list[position].imgUrl).error(R.color.gray).into(holder.binding.imageView)
    }

    override fun getItemCount() = list.size

//    @BindingAdapter("image")
//    fun setImageResource(image: ImageView, src:String) {
//        if (!src.isEmpty()) {
//            Glide.with(image).load(Constant.Service_IP+src).error(R.color.gray).into(image)
//        }else{
//            image.setImageResource(R.color.gray)
//        }
//    }
}